"""
利用Python进行数据分析

数据转换
"""
import numpy as np
from pandas import Series, DataFrame
import pandas as pd


def test_01():
    """
    移除重复数据
    :return:
    """
    data = DataFrame({'key1': ['one'] * 3 + ['two'] * 4,
                      'key2': [1, 1, 2, 3, 3, 4, 4]})
    print(data)
    # duplicated()返回一个布尔型Series, 表示各行是否是重复行
    print(data.duplicated())
    # 返回一个移除了重复行的DataFrame
    print(data.drop_duplicates())
    data['v1'] = range(7)
    # 根据key1列判断重复
    print(data.drop_duplicates(['key1']))
    # 保留最后一个
    print(data.drop_duplicates(['key1', 'key2'], take_lask=True))


def test_02():
    """
    利用函数或映射进行数据转换
    :return:
    """
    data = DataFrame({'food': ['bacon', 'pulled pork', 'bacon', 'Pastrami',
                               'corned beef', 'Bacon', 'pastrami', 'honey ham',
                               'nova lox'],
                      'ounces': [4, 3, 12, 6, 7.5, 8, 3, 5, 6]})
    # print(data)

    meat_to_animal = {
        'bacon': 'pig',
        'pulled pork': 'pig',
        'pastrami': 'cow',
        'corned beef': 'cow',
        'honey ham': 'pig',
        'nova lox': 'salmon'
    }

    # 先将food对应的字符串转换为小写，再进行映射
    # data['animal'] = data['food'].str.lower().map(meat_to_animal)
    # print(data)

    print(data['food'].map(lambda x: meat_to_animal[x.lower()]))


def test_03():
    """
    替换值
    :return:
    """
    data = Series([1., -999., 2., -999., -1000., 3.])
    # print(data)
    # print(data.replace(-999., np.nan))
    # print(data.replace([-999., -1000], np.nan))
    # print(data.replace([-999., -1000], [np.nan, 0]))
    print(data.replace({-999: np.nan, -1000: 0.}))


def test_04():
    """
    重命名轴索引
    :return:
    """
    data = DataFrame(np.arange(12).reshape((3, 4)),
                     index=['Ohio', 'Colorado', 'New York'],
                     columns=['one', 'two', 'three', 'four'])
    # print(data)

    # print(data.index.str.upper())

    # data.index = data.index.str.upper()
    # print(data)

    # 创建数据集的转换版（而不是修改原始数据）
    # print(data.rename(str.upper, axis='columns'))
    # print(data.rename(str.upper, axis='index'))

    # 结合字典型对象实现对部分轴标签的更新
    # print(data.rename(index={'Ohio': 'INDIANA'}, columns={'three': 'peekaboo'}))

    # 就地修改某个数据集
    data.rename(index={'Ohio': 'INDIANA'}, inplace=True)
    print(data)


def test_05():
    """
    离散化和面元划分
    :return:
    """
    # 划分不同年龄组
    # ages = [20, 22, 25, 27, 21, 23, 37, 31, 61, 45, 41, 32]
    # bins = [18, 25, 35, 60, 100]
    # cats = pd.cut(ages, bins)
    # cats = pd.cut(ages, bins, right=False)
    # print(cats)
    # print(pd.value_counts(cats))

    # 设置自己的面元名称
    # group_names = ['Youth', 'YoungAdult', 'MiddleAged', 'Senior']
    # cats = pd.cut(ages, bins, right=False, labels=group_names)
    # print(cats)
    # print(pd.value_counts(cats))

    # data = np.random.randn(20)
    # print(pd.cut(data, 4, precision=2))

    data = np.random.randn(1000)
    # cats = pd.qcut(data, 4)
    # print(cats)
    # print(pd.value_counts(cats))
    # 设置自定义的分位数
    print(pd.qcut(data, [0, 0.1, 0.5, 0.9, 1.]))


def test_06():
    """
    检测和过滤异常值
    :return:
    """
    np.random.seed(12345)
    data = DataFrame(np.random.randn(1000, 4))
    # print(data)
    # print(data.describe())

    # 某列中绝对值大小超过3的值
    # col = data[3]
    # print(col[np.abs(col) > 3])

    # 选出全部含有 超过3或-3的值
    # print(data[(np.abs(data) > 3).any(1)])

    # 将值限制在区间-3到3以内
    data[np.abs(data) > 3] = np.sign(data) * 3
    print(data.describe())


def test_07():
    """
    排列和随机采样
    :return:
    """
    # df = DataFrame(np.arange(5 * 4).reshape(5, 4))
    # print(df)
    # sampler = np.random.permutation(5)
    # print(sampler)
    # print(df)
    # print(df.take(sampler))
    # print(df.take(np.random.permutation(len(df))[:3]))

    bag = np.array([5, 7, -1, 6, 4])
    sampler = np.random.randint(0, len(bag), size=10)
    print(sampler)
    draws = bag.take(sampler)
    print(draws)


def test_08():
    """
    计算指标/哑变量
    :return:
    """
    # df = DataFrame({'key': ['b', 'b', 'a', 'c', 'a', 'b'],
    #                 'data1': range(6)})
    # print(df)
    # print(pd.get_dummies(df['key']))
    # dummies = pd.get_dummies(df['key'], prefix='key')
    # df_with_dummy = df[['data1']].join(dummies)
    # print(df_with_dummy)

    values = np.random.rand(10)
    print(values)
    bins = [0, 0.2, 0.4, 0.6, 0.8, 1]
    print(pd.get_dummies(pd.cut(values, bins)))


def main():
    # test_01()
    # test_02()
    # test_03()
    # test_04()
    # test_05()
    # test_06()
    # test_07()
    test_08()


if __name__ == '__main__':
    main()
